<?php

require_once( dirname(__FILE__).'/../libs/bedmintonlib.php' );

function validateEmailAddress( $emailAddress, & $outMessage )
{
    $db = Db::db();
    $dbConnection = $db->connection();
    if (is_null( $dbConnection ))
    {
        $outMessage = sprintf("'\"%s\"'", isEnglish() ? "Db connection error." : "Chyba pripojenia" );
        logError( __FUNCTION__ . ": Db connection error" );
        return false;
    }

    try
    {
        $statement = $dbConnection->prepare( "SELECT emailaddress FROM customers WHERE emailaddress = ?" );
        $statement->bindParam( 1, $emailAddress, PDO::PARAM_STR );
        $statement->execute();
        $email = $statement->fetchColumn();
        if ($email == $emailAddress)
        {
            $outMessage = sprintf("\"%s\"", isEnglish() ? "Login already exists." : "Login už existuje" );
            return false;
        }
    }
    catch(PDOException $e)
    {
        $outMessage = sprintf("\"%s\"", isEnglish() ? "Db error." : "Databázová chyba" );
        logError(  __FUNCTION__ . ": Db error $outMessage" );
        return false;
    }
    
    return true;
}

$valid = 'true';
if (isset( $_REQUEST['email'] ))
{
    $email = trim(strtolower($_REQUEST['email']));
    validateEmailAddress( $email, $valid );
    //    $valid = '"Thats already taken."';
}

echo $valid;

?>
